﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class DriverRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<Driver> GetAllData()
        {
            return from Driver in db.Drivers
                   orderby Driver.CivilID descending
                   select Driver;
        }

        public IQueryable<Driver> GetAllDataByStatus(string status)
        {
            return from Driver in db.Drivers
                   where Driver.DriverMissionStatusCode == status
                   orderby Driver.CivilID descending
                   select Driver;
        }

        public IQueryable<Driver> GetDataByName(string searchField, string searchOper, string searchString)
        {
            var ps = db.Drivers.Where(p => p.FirstName.Contains(searchString));
            if (searchField == "CivilID")
                ps = db.Drivers.Where(p => p.CivilID.Contains(searchString));
            if (searchField == "FirstName")
                ps = db.Drivers.Where(p => p.FirstName.Contains(searchString));
            if (searchField == "LastName")
                ps = db.Drivers.Where(p => p.LastName.Contains(searchString));
            if (searchField == "PassportNo")
                ps = db.Drivers.Where(p => p.PassportNo.Contains(searchString));
            if (searchField == "ArabicFirstName")
                ps = db.Drivers.Where(p => p.ArabicFirstName.Contains(searchString));
            if (searchField == "CountryCode")
                ps = db.Drivers.Where(p => p.CountryCode.Contains(searchString));
            if (searchField == "All")
                ps = db.Drivers.Where(p => p.CivilID.Contains(searchString) 
                                        || p.FirstName.Contains(searchString) 
                                        || p.LastName.Contains(searchString) 
                                        || p.PassportNo.Contains(searchString));

            return ps.AsQueryable();

        }


        public IQueryable<Driver> GetDataByStatus(string searchField, string searchOper, string searchString, string status)
        {
            var ps = db.Drivers.Where(p => p.FirstName.Contains(searchString));
            if (searchField == "CivilID")
                ps = db.Drivers.Where(p => p.CivilID.Contains(searchString));
            if (searchField == "FirstName")
                ps = db.Drivers.Where(p => p.FirstName.Contains(searchString));
            if (searchField == "LastName")
                ps = db.Drivers.Where(p => p.LastName.Contains(searchString));
            if (searchField == "PassportNo")
                ps = db.Drivers.Where(p => p.PassportNo.Contains(searchString));
            if (searchField == "ArabicFirstName")
                ps = db.Drivers.Where(p => p.ArabicFirstName.Contains(searchString));
            if (searchField == "CountryCode")
                ps = db.Drivers.Where(p => p.CountryCode.Contains(searchString));
            if (searchField == "All")
                ps = db.Drivers.Where(p => p.CivilID.Contains(searchString)
                                        || p.FirstName.Contains(searchString)
                                        || p.LastName.Contains(searchString)
                                        || p.PassportNo.Contains(searchString));

            //return ps.AsQueryable();
            return from Driver in ps
                   where Driver.DriverMissionStatusCode == status
                   select Driver;
        }


        public IQueryable<Driver> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllData().OrderByDescending(sortColumn);
                    else
                        return GetAllData().OrderBy(sortColumn);
                else
                    return GetAllData();
            }
        }

        public IQueryable<Driver> GetAllDataByStatus(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString, string status)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByStatus(searchField, searchOper, searchString, status).OrderByDescending(sortColumn);
                    else
                        return GetDataByStatus(searchField, searchOper, searchString, status).OrderBy(sortColumn);
                else
                    return GetDataByStatus(searchField, searchOper, searchString, status).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllDataByStatus(status).OrderByDescending(sortColumn);
                    else
                        return GetAllDataByStatus(status).OrderBy(sortColumn);
                else
                    return GetAllDataByStatus(status);
            }
        }



        public Driver GetByID(string id)
        {
            return db.Drivers.SingleOrDefault(Driver => Driver.CivilID == id);
        }

        //Inset
        public void Add(Driver Driver)
        {
            //if (Driver.StatusID == 0 )
            //    Driver.StatusID = 1;
            Driver.InsertedUser = HttpContext.Current.User.Identity.Name;
            Driver.InsertedDate = DateTime.Now;
            Driver.UpdatedUser = HttpContext.Current.User.Identity.Name;
            Driver.UpdatedDate = DateTime.Now;
            Driver.DataVersion = 1;

            db.Drivers.AddObject(Driver);
        }

        //Update
        public void Update(Driver Driver)
        {

            Driver originalDriver = GetByID(Driver.CivilID);
            
            Driver.DriverMissionStatusCode = originalDriver.DriverMissionStatusCode;
            Driver.InsertedUser = originalDriver.InsertedUser;
            Driver.InsertedDate = originalDriver.InsertedDate;
            Driver.UpdatedUser = HttpContext.Current.User.Identity.Name;
            Driver.UpdatedDate = DateTime.Now;
            Driver.DataVersion = ++originalDriver.DataVersion;
  
            db.Drivers.ApplyCurrentValues(Driver);
        }

        //Delete
        public void Delete(Driver Driver)
        {
            db.Drivers.DeleteObject(Driver);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}